Skip to content

chore(iterators): create Iterator module and migrate iterators to use it #1392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2022

Conversation

kyazdani42
Copy link
Member

Missing one iterator (see TODO comment), because i'm not sure about the interface. This is a first attempt to clean up the repetitive code for iterators.
Not sure this is the best implementation i could go with, but it works for now.

@kyazdani42 kyazdani42 requested a review from alex-courtis July 3, 2022 13:37
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic.

  • NOP case Iterator.new(nodes):consume() tested ok.
  • Iterator.new(nodes) fails but I don't think that matters.
  • Tested expand-all as it is a big change
  • Reviewed other usages but they are all trivial refactors and did not test

local Iterator = {}
Iterator.__index = Iterator

function Iterator.new(nodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a NodeIterator; we can call it that. More iterators can be added in the future, including a generic one.

All functions except match and apply are node specific. The could go in an AbstractIterator parent class, but that can come when more iterators are added.

if node.open then
if iterate(node) then
return true
Iterator.new(parent.nodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the fluid nature. This is classic builder pattern. Perhaps we could make the naming more concise e.g.

NodeIterator.builder(parent.nodes)
  :hidden()
  :matcher(...)
  :applier(...)
  :recursor(...)
  :build()()

The :build()() is a bit redundant and implies that we have an intermediate NodeIteratorBuilder which doesn't add much value, so :consume() or iterate() is appropriate.

end

function Iterator:allow_hidden()
self._filter_hidden = function(_)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit this could just be a boolean member.

@kyazdani42 kyazdani42 force-pushed the chore/refactor-iterators branch from b17b22f to 10aa799 Compare July 4, 2022 08:25
@kyazdani42 kyazdani42 requested a review from alex-courtis July 4, 2022 08:25
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful

@kyazdani42 kyazdani42 merged commit f43b8af into master Jul 4, 2022
@kyazdani42 kyazdani42 deleted the chore/refactor-iterators branch July 4, 2022 12:13
Almo7aya pushed a commit to Almo7aya/nvim-tree.lua that referenced this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants